# Maintainer: David Macek <david.macek.0@gmail.com>

_realname=aria2
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=1.37.0
pkgrel=2
pkgdesc="A multi-protocol & multi-source, cross platform download utility (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url='https://aria2.github.io/'
msys2_repository_url='https://github.com/aria2/aria2/'
msys2_references=(
  'archlinux: aria2'
  "cpe: cpe:/a:aria2_project:aria2"
  "cpe: cpe:/a:tatsuhiro_tsujikawa:aria2"
)
license=('spdx:GPL-2.0-or-later')
depends=("${MINGW_PACKAGE_PREFIX}-gcc-libs"
         "${MINGW_PACKAGE_PREFIX}-gettext"
         "${MINGW_PACKAGE_PREFIX}-c-ares"
         "${MINGW_PACKAGE_PREFIX}-libiconv"
         "${MINGW_PACKAGE_PREFIX}-libssh2"
         "${MINGW_PACKAGE_PREFIX}-libxml2"
         "${MINGW_PACKAGE_PREFIX}-openssl"
         "${MINGW_PACKAGE_PREFIX}-sqlite3"
         "${MINGW_PACKAGE_PREFIX}-zlib")
makedepends=("${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-autotools"
             "${MINGW_PACKAGE_PREFIX}-pkgconf")
checkdepends=("${MINGW_PACKAGE_PREFIX}-cppunit")
optdepends=("${MINGW_PACKAGE_PREFIX}-ruby: aria2rpc and aria2mon")
source=("https://github.com/aria2/aria2/releases/download/release-${pkgver}/aria2-${pkgver}.tar.xz"
        "0001-less-strict-gettext-checks.patch")
sha256sums=('60a420ad7085eb616cb6e2bdf0a7206d68ff3d37fb5a956dc44242eb2f79b66b'
            'f2b27d92c6976ad20cc7a43aaa75576911a2fa5291dbb360cd5fd01d3114ad25')

prepare() {
  cd "${_realname}-${pkgver}"

  patch -Np1 -i ../0001-less-strict-gettext-checks.patch

  autoreconf -ifv
}

build() {
  mkdir -p build-${MSYSTEM} && cd build-${MSYSTEM}

  # disabling gnutls and wintls so we will fall back to openssl
  ../${_realname}-${pkgver}/configure \
    --prefix=${MINGW_PREFIX} \
    --with-bashcompletiondir=${MINGW_PREFIX}/share/bash-completion/completions \
    --build=${MINGW_CHOST} \
    --host=${MINGW_CHOST} \
    --enable-static \
    --enable-shared \
    --without-gnutls \
    --without-wintls \
    --enable-libaria2

  make
}

check() {
  cd build-${MSYSTEM}
  make check || true
}

package() {
  cd "build-${MSYSTEM}"
  make DESTDIR="${pkgdir}" install

  cd "../${_realname}-${pkgver}"

  install -Dm0755 doc/xmlrpc/aria2rpc "${pkgdir}"${MINGW_PREFIX}/bin/aria2rpc
  install -Dm0755 doc/xmlrpc/aria2mon "${pkgdir}"${MINGW_PREFIX}/bin/aria2mon
  echo '@ruby -- "%~dp0aria2rpc" %*' > "${pkgdir}"${MINGW_PREFIX}/bin/aria2rpc.cmd
  echo '@ruby -- "%~dp0aria2mon" %*' > "${pkgdir}"${MINGW_PREFIX}/bin/aria2mon.cmd
  rm "${pkgdir}"${MINGW_PREFIX}/share/doc/${_realname}/xmlrpc/aria2{rpc,mon}
}
